package com.dtsz.m2.ui.activity;

import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import androidx.annotation.Nullable;

import com.android.volley.Request;
import com.dtsz.m2.R;
import com.dtsz.m2.config.StaticConfig;
import com.dtsz.m2.config.UrlConfig;
import com.dtsz.m2.ui.base.BaseActivity;
import com.dtsz.m2.ui.inter.OnPasswordInputFinish;
import com.dtsz.m2.ui.model.RegisterVo;
import com.dtsz.m2.ui.model.Users;
import com.dtsz.m2.ui.widget.PopEnterMyResetPassword;
import com.dtsz.m2.ui.widget.PopEnterResetPassword;
import com.dtsz.m2.utils.MyCountdownUtils;
import com.dtsz.m2.utils.RSAEncrypt;
import com.dtsz.m2.utils.StringUtils;
import com.dtsz.m2.volley.state.HUCState;

public class RetrievePasswordActivity extends BaseActivity implements OnPasswordInputFinish {
    private Button vtVerification;
    private EditText etCode, etPhone;
    private MyCountdownUtils myCountdownUtils;
    private RegisterVo registerVo = new RegisterVo();

    private PopEnterMyResetPassword popEnterMyResetPassword;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        setContentView(R.layout.activity_retrieve_password);
        super.onCreate(savedInstanceState);
        tvActionbarTitle.setText("修改资金密码");
        String phone = getSharedPreferences("phone");
        registerVo.setPhone(phone);
        etPhone.setText(phone);
    }

    @Override
    public void initView() {
        super.initView();
        vtVerification = findViewById(R.id.bt_verification);
        etCode = findViewById(R.id.et_code);
        etPhone = findViewById(R.id.et_phone);

    }

    @Override
    public void initClick() {
        super.initClick();
        findViewById(R.id.bt_save).setOnClickListener(this);
        vtVerification.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.bt_verification:
                getCode();
                break;
            case R.id.bt_save:
                String code = etCode.getText().toString();
                if (StringUtils.isEmpty(code)) {
                    showToast("验证码不能为空");
                    return;
                }
                getCodeRes();
                break;
        }
    }

    @Override
    public void onSuccessfulStrType(int inputType, Object response) {
        super.onSuccessfulStrType(inputType, response);
        switch (inputType) {
            case HUCState.HUC_REQEUST_CODE_1:
                String reponseStr1 = (String) response;
                if ("验证码已发送，请注意查收".equals(reponseStr1)) {
                    myCountdownUtils = new MyCountdownUtils(vtVerification, 60000, 1000);
                    myCountdownUtils.start();
                }
                showToast(reponseStr1);
                break;
            case HUCState.HUC_REQEUST_CODE_2:
                String reponseStr = (String) response;
                if ("验证通过".equals(reponseStr)) {
                    popEnterMyResetPassword = new PopEnterMyResetPassword(this, this);
                    // 显示窗口
                    popEnterMyResetPassword.showAtLocation(vtVerification,
                            Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
                }
                showToast(reponseStr);
                break;
            case HUCState.HUC_REQEUST_CODE_3:
                String msg = (String) response;
                showToast(msg);
                if ("设置成功".equals(msg)) {
                    finish();
                    popEnterMyResetPassword.dismiss();
                }
                break;
        }
        loadDismiss();

    }

    private void getCode() {
        loading();
        registerVo.setToken(pageVo.getToken());
        registerVo.setType(StaticConfig.MSGCODE);
        hucRequestInterfase.executeStr(this, RegisterVo.class,
                HUCState.HUC_REQEUST_CODE_1, Request.Method.POST, UrlConfig.CODE_URL,
                hucParameInterfase.getRequestParameter(registerVo), true);
    }

    //修改密码
    private void getCodeRes() {
        loading();
        registerVo.setCode(etCode.getText().toString());
        hucRequestInterfase.executeStr(this, RegisterVo.class,
                HUCState.HUC_REQEUST_CODE_2, Request.Method.POST, UrlConfig.RESETRSA_URL,
                hucParameInterfase.getRequestParameter(registerVo), true);
    }

    @Override
    public void inputFinish(String password) {
        setPassword(password);
    }

    private void setPassword(String pwd) {
        loading();
        pageVo.setRsa(RSAEncrypt.encrypt(pwd));
        hucRequestInterfase.executeStr(this, Users.class,
                HUCState.HUC_REQEUST_CODE_3, Request.Method.POST, UrlConfig.RSA_URL,
                hucParameInterfase.getRequestParameter(pageVo), true);
    }
}